package com.cheyun.netsalev3.view

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.CustomerEditTagAdapter
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.databinding.ActivityAddClueBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.AddClueActivityViewModel
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout

class AddClueActivity : BaseActivity<ActivityAddClueBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_add_clue
    }

    override fun initViewModel(): ViewModel? {
        var name = intent.getStringExtra("name") ?: ""
        var phone = intent.getStringExtra("phone") ?: ""
        var sex = intent.getIntExtra("sex", 0)
        var come = ""
        if(intent.hasExtra("come")) {
            come = intent.getStringExtra("come")
        }
        var keliudata: Data? = null
        if(intent.hasExtra("keliudata")) {
            keliudata = (intent.getSerializableExtra("keliudata") as Data)
        }
        var ed = EditClueParam()
        ed.uname = name
        ed.phone = phone
        ed.effective = "1"
        ed.sex = sex.toString()
        var factory = InjectorUtils.getAddClueActivityViewModelFactory(ed,come,keliudata)
        return ViewModelProviders.of(this, factory).get(AddClueActivityViewModel::class.java)
    }

    override fun initData() {
        binding.viewModel?.getFollowStatus()
        if(binding.viewModel?.editClueParam?.sex.equals("1")){
            binding.nanBut.isChecked = true
        }else if(binding.viewModel?.editClueParam?.sex.equals("2")){
            binding.nvBut.isChecked = true
        }else{
            binding.nullBut.isChecked = true
        }
        binding.viewModel?.focusable?.observe(this, Observer {
            binding.etFollow.setFocusable(it)
            binding.etName.setFocusable(it)
            binding.phoneEdt.setFocusable(it)
        })
        binding.viewModel?.editBtnEnable?.observe(this, Observer {
            binding.addBut.isEnabled = it
        })
        binding.viewModel?.customerOwnerTag?.observe(this, Observer {
            customerEditTagAdapter?.setData(it)
        })
        binding.viewModel?.userTag?.observe(this, Observer {
            customerTagAdapter?.setData(it)
        })
        binding.viewModel?.editBtnEnable?.observe(this, Observer {
            binding.addBut.isEnabled = it
        })

    }

    var customerEditTagAdapter:CustomerEditTagAdapter? = null
    var customerTagAdapter:CustomerEditTagAdapter? = null

    override fun initView() {
        binding.radioBut.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.nanBut -> binding.viewModel?.setSex(1)
                R.id.nvBut -> binding.viewModel?.setSex(2)
            }
        }

        binding.phoneEdt.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (FunctionUtils.isPhone2(binding.phoneEdt.text.toString(), this@AddClueActivity)) {
                    binding.viewModel?.getPhone(binding.phoneEdt.text.toString(), this@AddClueActivity)
                }
            }
        })

        customerTagAdapter = CustomerEditTagAdapter(this,null,true)
        binding.userTagFlow.setAdapter(customerTagAdapter)

        customerEditTagAdapter = CustomerEditTagAdapter(this,null ,true)
        binding.customerTagFlow.setAdapter(customerEditTagAdapter)

        binding.customerTagFlow.setOnTagClickListener(object : TagFlowLayout.OnTagClickListener{
            override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
//                customerEditTagAdapter?.notifyDataChanged()
                binding.viewModel?.removeCustomerEditTag(position)
                return true
            }
        })

        binding.userTagFlow.setOnTagClickListener(object : TagFlowLayout.OnTagClickListener{
            override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
                binding.viewModel?.removeUserTag(position)
                return true
            }
        })

        binding.viewModel?.setActivity(this)
    }

    override fun onResume() {
        super.onResume()
        binding.addBut.isEnabled = true
        binding.viewModel?.enableAdd = true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 102) {
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                if(requestCode == 101){
                    //首选车，意向车
                    binding.viewModel?.setYixiangCarData(carBrandParam)
                    binding.txtShouxuanche.text = carBrandParam.seriesname + " "+ carBrandParam.specname
                }else if(requestCode == 102){
                    //备选车
                    binding.viewModel?.setBeixuanCarData(carBrandParam)
                    binding.txtBeixuanche.text = carBrandParam.seriesname + " "+ carBrandParam.specname
                }else{
                    binding.viewModel?.setCarData(carBrandParam)
//                binding.ebrandname.text = carBrandParam.brandname
//                    binding.eseriesname.text = carBrandParam.seriesname
//                    binding.especname.text = carBrandParam.specname
//                    if(carBrandParam.spec_select > 0){
//                        binding.especxinghao.visibility = View.VISIBLE
//                        binding.viewModel?.editClueParam?.needSpec = true
//                    }else{
//                        binding.especxinghao.visibility = View.GONE
//                        binding.viewModel?.editClueParam?.needSpec = false
//                    }
                }
            }
        }
        if (resultCode == 103) {
            data?.apply {
                var shen = getSerializableExtra("shen") as CarPinPaParam
                var shi = getSerializableExtra("shi") as CarPinPaParam
                var que = getSerializableExtra("que") as CarPinPaParam
                var show = shen.title + " " + shi.title + " " + que.title
                var id = shen.id.toString() + "," + shi.id.toString() + "," + que.id.toString()
                binding.cityText.text = shen.title + " " + shi.title
                binding.regionText.text = que.title
                binding.viewModel?.setCityData(show, id)
            }
        }
        if (resultCode == 104) {
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                binding.viewModel?.setCarData2(carBrandParam)
                binding.txtXianyouche.text = carBrandParam.brandname + " " + carBrandParam.seriesname
            }
        }
        if (resultCode == 105) {
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                binding.viewModel?.addCompetitor(carBrandParam)
                binding.txtDuibiche.text = carBrandParam.brandname + " " + carBrandParam.seriesname
            }
        }
        if(resultCode == 106){
            data?.apply {
//                var ids = getStringExtra("ids")
                var list = getSerializableExtra("list") as ArrayList<DialogParam>
                binding.viewModel?.setUserTag(list)

            }
        }
    }
}
